package com.baidu.infinity.ui.view

import android.animation.ObjectAnimator
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.animation.AnticipateInterpolator
import android.view.animation.BounceInterpolator
import android.widget.FrameLayout
import androidx.fragment.app.Fragment
import com.baidu.infinity.databinding.LayoutAlterViewBinding
import com.baidu.infinity.databinding.LayoutInputViewBinding
import com.baidu.infinity.ui.util.delayTask
import com.baidu.infinity.ui.util.dp2pxF
import java.util.zip.Inflater

class AlterView: FrameLayout{
    private lateinit var binding: LayoutAlterViewBinding
    private val upAnimator: ObjectAnimator = ObjectAnimator.ofFloat(
        this,
        "translationY",
        dp2pxF(150),
        0f
    ).apply {
        interpolator = AnticipateInterpolator()
    }
    private val downAnimator: ObjectAnimator = ObjectAnimator.ofFloat(
        this,
        "translationY",
        0f,
        dp2pxF(150)
    ).apply {
        interpolator = BounceInterpolator()
    }

    constructor(context: Context):super(context){
        initView()
    }
    //attrs:AttributeSet?参数就是用来接收自定义属性的值
    constructor(context: Context, attrs: AttributeSet?):super(context,attrs) {
        initView()
    }
    fun initView(){
        val inflater = LayoutInflater.from(context)
        binding = LayoutAlterViewBinding.inflate(inflater)
        val layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)
        layoutParams.gravity = Gravity.CENTER
        addView(binding.root,layoutParams)

    }
    private fun startAnimation(){
        downAnimator.start()
        delayTask(1500){
            upAnimator.start()
        }
    }
    fun showMessage(msg:String){
        binding.textView.text = msg
        binding.textView.setTextColor(Color.BLACK)
        startAnimation()
    }
    fun showErroeMessage(msg:String){
        binding.textView.text = msg
        binding.textView.setTextColor(Color.RED)
        startAnimation()
    }
}